﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using WW.NActiveRecord.ContractExceptions;
using System.Reflection;

namespace WW.NActiveRecord.Internal
{
    /// <summary>
    /// 实体成员接口,负责对实体成员初始化其宿主对象
    /// </summary>
    internal interface IEntityMember{
        void Init(FieldInfo fieldInfo, Entity owner);
    }

    /// <summary>
    /// 实体成员帮助对象
    /// </summary>
    internal sealed class EntityMember : IEntityMember
    {
        private Entity owner;
        private FieldInfo fieldInfo;

        //2阶段初始化器
        public void Init(FieldInfo fieldInfo, Entity owner){
            this.fieldInfo = fieldInfo;
            this.owner = owner;
        }
        
        internal Entity Owner{
            get{
                Debug.Assert(owner!=null,"没有调用Init()方法");//internal assert
                return owner;
            }
            set { owner = value; }
        }

        /// <summary>
        /// 实体中定义的对应此列的对象字段
        /// </summary>
        internal  FieldInfo FieldInfo{
            get{
                Debug.Assert(owner != null, "没有调用Init()方法");//internal assert
                return fieldInfo;
            }
        }

        internal string FieldName{
            get { return FieldInfo.Name; }
        }

        public override string ToString() {
            return string.Format("{{field:{0}}}");
        }
    }
}